<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <!-- uploadify -->
    <script type="text/javascript" src="/js/uploadify/jquery.uploadify.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/js/uploadify/uploadify.css" />

</head>

<body class="hold-transition skin-red sidebar-mini">
<!-- .box-body -->

<div class="box-header with-border">
    <h3 class="box-title">广告管理</h3>
</div>

<div class="box-body">

    <!-- 数据表格 -->
    <div class="table-box">

        <!--工具栏-->
        <div class="pull-left">
            <div class="form-group form-inline">
                <div class="btn-group">
                    <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                    <button type="button" class="btn btn-default" title="删除" onclick="deleteBatch()"><i class="fa fa-trash-o"></i> 删除</button>
                    <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                    <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                    <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                </div>
            </div>
        </div>
        <div class="box-tools pull-right">
            <div class="has-feedback">
                名称：<input id="title2">	<button class="btn btn-default" onclick="getData()">查询</button>
            </div>
        </div>
        <!--工具栏/-->

        <!--数据列表-->
        <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
            <thead>
            <tr>
                <th class="" style="padding-right:0px">
                    <input id="selall" type="checkbox" class="icheckbox_square-blue">
                </th>
                <th class="sorting_asc">广告ID</th>

                <th class="sorting">标题</th>
                <th class="sorting">广告连接地址</th>
                <th class="sorting">图片地址</th>
                <th class="sorting">广告类型</th>
                <th class="sorting">开始时间</th>
                <th class="sorting">结束时间</th>
                <th class="sorting">金额</th>
                <th class="text-center">操作</th>
            </tr>
            </thead>
            <tbody id="content">

            </tbody>
        </table>
        <!--数据列表/-->
    </div>
    <!-- 数据表格 /-->
</div>
<!-- /.box-body -->


<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel">广告编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped"  width="800px">
                    <tr>
                        <td>标题</td>
                        <td><input  class="form-control" placeholder="标题" id="title">  </td>
                    </tr>

                    <tr>
                        <td>广告链接地址</td>
                        <td><input  class="form-control" placeholder="link" id="link">  </td>
                    </tr>
                    <tr>
                        <td>图片链接地址</td>
                        <td><input  class="form-control" placeholder="图片链接地址" id="linkUrl">  </td>
                    </tr>
                    <tr>
                        <td>广告分类</td>
                        <td>
                            <select class="form-control" id="cate">
                                <option th:each="cate:${list}" th:value="${cate.id}">[[${cate.name}]]</option>
                            </select>
                        </td>
                    </tr>

                    <tr>
                        <td>开始时间</td>
                        <td><input type="datetime" class="form-control" placeholder="开始时间" id="startTime">  </td>
                    </tr>
                    <tr>
                        <td>结束时间</td>
                        <td>
                        <input type="datetime" class="form-control" placeholder="结束时间" id="endTime">
                        </td>
                    </tr>
                    <tr>
                        <td>价格</td>
                        <td>
                            <input  class="form-control" placeholder="价格" id="payPrice">
                        </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addData()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>

<!-- 编辑窗口 -->
<div class="modal fade" id="editModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h3 id="myModalLabel2">广告编辑</h3>
            </div>
            <div class="modal-body">

                <table class="table table-bordered table-striped"  width="800px">
                    <tr>
                        <td>标题</td>
                        <td><input  class="form-control" placeholder="标题" id="titled">  </td>
                    </tr>

                    <tr>
                        <td>广告链接地址</td>
                        <td><input  class="form-control" placeholder="link" id="link2">  </td>
                    </tr>
                    <tr>
                        <td>图片链接地址</td>
                        <td><input  class="form-control" placeholder="图片链接地址" id="linkUrl2">  </td>
                    </tr>
                    <tr>
                    <tr>
                        <td>广告分类</td>
                        <td>
                            <input type="hidden" id="CID">
                            <select class="form-control" id="cate2">
                                <option th:each="cate:${list}" th:value="${cate.id}">[[${cate.name}]]</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>开始时间</td>
                        <td><input type="datetime" class="form-control" placeholder="开始时间" id="startTime2">  </td>
                    </tr>
                    <tr>
                        <td>结束时间</td>
                        <td>
                            <input type="datetime" class="form-control" placeholder="结束时间" id="endTime2">
                        </td>
                    </tr>
                    <tr>
                        <td>价格</td>
                        <td>
                            <input  class="form-control" placeholder="价格" id="payPrice2">
                        </td>
                    </tr>
                </table>

            </div>
            <div class="modal-footer">
                <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="changeData()">保存</button>
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">

    $(function () {
        getData();
    });
    function getData() {
        $.ajax({
            url: "/content/getContentInfo" ,
            type: "get",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 200){
                    var list = result.data;
                    var info = "";
                    $(list).each(function (i,e) {
                        info += "<tr>";
                        info += '<td><input  type="checkbox" value="'+e.id+'" name="box"></td>';
                        info += '<td>'+e.id+'</td>';
                        info += '<td>'+e.title+'</td>';
                        info +='<td>'+e.link+'</td>';
                        info += '<td>'+e.linkUrl+'</td>';
                        info += '<td>'+getRank(e.contentTypeId)+'</td>';
                        info += '<td>'+e.startTime+'</td>';
                        info += '<td>'+e.endTime+'</td>';
                        info += '<td>'+e.payPrice+'</td>';
                        info += '<td class="text-center">';
                        info += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal2"  onclick="updateBrand('+e.id+')">修改</button>';
                        info += '</td>';
                        info += '</tr>';
                    });
                    $("#content").html(info);
                }
            },
            error: function () {
                alert("系统异常,请联系管理员");

            }
        })
    }




    function getRank(id) {
        var name;
        $.ajax({
            url: "/contenttype/toUpdate/"+id,
            type: "post",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 200){
                    var itemcat = result.data;
                    name = itemcat.name;
                }
            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        });
        return name;
    }

    function addData() {
        var title = $("#title").val();
        var link = $("#link").val();
        var linkUrl = $("#linkUrl").val();
        var contentTypeId = $("#cate2").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var payPrice = $("#payPrice").val();
        $.ajax({
            url:"/content/addContentInfo",
            type:"post",
            contentType: 'application/json; charset=UTF-8',
            data:JSON.stringify({
                title:title,link:link,linkUrl:linkUrl,
                contentTypeId:contentTypeId,startTime:startTime,
                endTime:endTime,payPrice:payPrice
            }),
            dataType:"json",
            async:false,
            success:function(result){
                if(result.code == 200){
                    window.location.reload();
                }

            },
            error:function(){
                alert("系统异常，联系管理员");
            }
        });

    }

    function changeData() {
        var id = $("#CID").val();
        var title = $("#titled").val();
        var link = $("#link2").val();
        var linkUrl = $("#linkUrl2").val();
        var contentTypeId = $("#cate2").val();
        var startTime = $("#startTime2").val();
        var endTime = $("#endTime2").val();
        var payPrice = $("#payPrice2").val();
        $.ajax({
            url:"/content/updateById",
            type:"post",
            contentType: 'application/json; charset=UTF-8',
            data:JSON.stringify({
                id:id,title:title,link:link,linkUrl:linkUrl,
                contentTypeId:contentTypeId,startTime:startTime,
                endTime:endTime,payPrice:payPrice
            }),
            dataType:"json",
            async:false,
            success:function(result){
                if(result.code == 200){
                    window.location.reload();
                }

            },
            error:function(){
                alert("系统异常，联系管理员");
            }
        });
    }

    function updateBrand(id) {
        $.ajax({
            url: "/content/toUpdate/"+id,
            type: "post",
            data: {},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 200){
                    var content = result.data;
                    $("#CID").val(content.id);
                    $("#titled").val(content.title);
                    $("#link2").val(content.link);
                    $("#linkUrl2").val(content.linkUrl);
                    $("#cate2").val(content.contentTypeId);
                    $("#startTime2").val(content.startTime);
                    $("#endTime2").val(content.endTime);
                    $("#payPrice2").val(content.payPrice);
                }

            },
            error: function () {
                alert("系统异常,请联系管理员");
            }
        })
    }

    function deleteBatch() {
        var ids = [];
        $("[name=box]:checked").each(function (i,e) {
            ids.push(e.value);
        });

        if(ids.length <= 0){
            alert("请选择需要删除的行");
            return;
        }

        $.ajax({
            url:"/content/deleteBatch/"+ids,
            type : "post",
            data:{},
            dataType : "json",
            async : false,//同步
            success : function(result) {
                if(result.code == 200){
                    window.location.reload();
                }

            },
            error : function() {
                alert("系统异常,请联系管理员");
            }
        })

    }
</script>
</html>